//////////////////////////////////////////////////////////////////////////////////
//	Copyright 2011 by Yosef Grabivker, ryuho@homenetworksoft.com
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
//////////////////////////////////////////////////////////////////////////////////

#include <wx/wx.h>
#include <wx/textdlg.h>
#include "listselectorpanel.h"

enum cIDS
{
	cIDListBox = 13467,
	cIDAdd,
	cIDRemove
};

BEGIN_EVENT_TABLE(CListSelectorPanel, wxPanel)
	EVT_UPDATE_UI(cIDRemove,	CListSelectorPanel::OnRemoveUI)
	EVT_BUTTON(cIDAdd,			CListSelectorPanel::OnAdd)
	EVT_BUTTON(cIDRemove,		CListSelectorPanel::OnRemove)
END_EVENT_TABLE()



CListSelectorPanel::CListSelectorPanel(wxWindow* pParent, const wxString& strTitle):
	wxPanel( pParent, wxID_ANY)
{
	int nBorder = 10;
	int nFlags  = wxALIGN_CENTER|wxALL;

	wxStaticBoxSizer* sizer = new wxStaticBoxSizer( new
		wxStaticBox(this, wxID_ANY, strTitle), wxVERTICAL);

	m_pListBox = new wxListBox(this, cIDListBox, wxDefaultPosition, wxDefaultSize,  NULL);
	sizer->Add( m_pListBox, 1, nFlags|wxGROW, nBorder );


	wxBoxSizer* sizerBtn = new wxBoxSizer(wxHORIZONTAL);

	sizerBtn->AddSpacer(nBorder);

	wxButton* pBtnAdd = new wxButton(this, cIDAdd, _("Add"), wxDefaultPosition, wxDefaultSize, wxBU_EXACTFIT|wxNO_BORDER );
	sizerBtn->Add( pBtnAdd, 0, wxALIGN_LEFT, 15 );

	sizerBtn->AddStretchSpacer();

	wxButton* pBtnRemove = new wxButton(this, cIDRemove, _("Remove"), wxDefaultPosition, wxDefaultSize, wxBU_EXACTFIT|wxNO_BORDER );
	sizerBtn->Add( pBtnRemove, 0, wxALIGN_RIGHT, 15 );

	sizerBtn->AddSpacer(nBorder);

	sizer->Add( sizerBtn, 0, wxLeft|wxRight|wxGROW, 15 );

	SetSizer( sizer );
}

CListSelectorPanel::~CListSelectorPanel(void)
{
}


void CListSelectorPanel::SetList(const TEXTLIST& list)
{
	m_pListBox->Clear();

	for(size_t i=0;i<list.size();++i)
	{
		m_pListBox->Append( wxString::FromAscii(list[i].c_str()) );
	}
}


void CListSelectorPanel::OnRemoveUI(wxUpdateUIEvent& event)
{
	event.Enable( wxNOT_FOUND != m_pListBox->GetSelection() );
}

void CListSelectorPanel::OnAdd(wxCommandEvent& ev)
{
	wxTextEntryDialog dlg(this, wxT("") );

	if ( wxID_OK == dlg.ShowModal() )
	{
		wxString strRes = dlg.GetValue();

		if ( !strRes.empty() )
		{
			int nPos = m_pListBox->Append(strRes);
			m_pListBox->Select( nPos );

			SyncList();
		}
	}
}

void CListSelectorPanel::OnRemove(wxCommandEvent& ev)
{
	int nSel = m_pListBox->GetSelection();

	if ( wxNOT_FOUND != nSel )
	{
		m_pListBox->Delete(nSel);

		SyncList();
	}
}

void CListSelectorPanel::SyncList()
{
	wxArrayString arr = m_pListBox->GetStrings();

	m_list.erase( m_list.begin(), m_list.end() );

	for (size_t i=0;i<arr.size();++i)
	{
		m_list.push_back( (const char*)arr[i].ToUTF8() );
	}
}

TEXTLIST CListSelectorPanel::GetList()
{
	SyncList();
	return m_list;
}